package com.yssoft.service.sales;

import com.yssoft.entity.core.TPersonnel;
import com.yssoft.entity.core.TcCompanyModify;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.core.TcCompanyModifyMapper;
import com.yssoft.shiro.MyRealm;
import com.yssoft.utils.UserHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TcCompanyModifyService {
    @Resource
    private TcCompanyModifyMapper modifyMapper;
    @Resource
    private TPersonnelMapper personnelMapper;

    public List<TcCompanyModify> queryCompanyModifyList(Integer companyId) {
        return modifyMapper.queryCompanyModifyList(companyId);
    }

    public boolean addTcCompanyModify(TcCompanyModify companyModify) {
        MyRealm.ShiroUser user = UserHelper.getCurrentUser();
        Integer creatorId = null;
        String creatorName = null;
        Integer deptId = null;
        String deptName = null;

        if (user != null) {
            creatorId = user.getPersonId();
            creatorName = user.getPersonName();
            deptId = user.getDeptId();
            deptName = user.getDeptName();
        }

        if (creatorId != null) {
            TPersonnel tPersonnel = personnelMapper.selectByPrimaryKey(creatorId);
            if (tPersonnel != null) {
                creatorId = tPersonnel.getId();
                creatorName = tPersonnel.getName();
            }
        } else if (user != null) {
            creatorId = user.getId();
            creatorName = user.getName();
        }

        companyModify.setCreatorId(creatorId);
        companyModify.setCreator(creatorName);
        companyModify.setDeptId(deptId);
        companyModify.setDeptName(deptName);
        return modifyMapper.insertCompanyModify(companyModify) > 0;
    }


}
